package edu.unf.cen6940.service;

import edu.unf.cen6940.dao.GeneratedproductDAO;
import edu.unf.cen6940.dao.ThesisDAO;

import edu.unf.cen6940.domain.Generatedproduct;
import edu.unf.cen6940.domain.Thesis;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Generatedproduct entities
 * 
 */

@Service("GeneratedproductService")
@Transactional
public class GeneratedproductServiceImpl implements GeneratedproductService {

	/**
	 * DAO injected by Spring that manages Generatedproduct entities
	 * 
	 */
	@Autowired
	private GeneratedproductDAO generatedproductDAO;

	/**
	 * DAO injected by Spring that manages Thesis entities
	 * 
	 */
	@Autowired
	private ThesisDAO thesisDAO;

	/**
	 * Instantiates a new GeneratedproductServiceImpl.
	 *
	 */
	public GeneratedproductServiceImpl() {
	}

	/**
	 * Load an existing Generatedproduct entity
	 * 
	 */
	@Transactional
	public Set<Generatedproduct> loadGeneratedproducts() {
		return generatedproductDAO.findAllGeneratedproducts();
	}

	/**
	 * Delete an existing Generatedproduct entity
	 * 
	 */
	@Transactional
	public void deleteGeneratedproduct(Generatedproduct generatedproduct) {
		generatedproductDAO.remove(generatedproduct);
		generatedproductDAO.flush();
	}

	/**
	 */
	@Transactional
	public Generatedproduct findGeneratedproductByPrimaryKey(Integer generatedProductId) {
		return generatedproductDAO.findGeneratedproductByPrimaryKey(generatedProductId);
	}

	/**
	 * Delete an existing Thesis entity
	 * 
	 */
	@Transactional
	public Generatedproduct deleteGeneratedproductThesises(Integer generatedproduct_generatedProductId, Integer related_thesises_thesisId) {
		Generatedproduct generatedproduct = generatedproductDAO.findGeneratedproductByPrimaryKey(generatedproduct_generatedProductId, -1, -1);
		Thesis related_thesises = thesisDAO.findThesisByPrimaryKey(related_thesises_thesisId, -1, -1);

		generatedproduct.getThesises().remove(related_thesises);
		related_thesises.getGeneratedproducts().remove(generatedproduct);
		generatedproduct = generatedproductDAO.store(generatedproduct);
		generatedproductDAO.flush();

		related_thesises = thesisDAO.store(related_thesises);
		thesisDAO.flush();

		thesisDAO.remove(related_thesises);
		thesisDAO.flush();

		return generatedproduct;
	}

	/**
	 * Return all Generatedproduct entity
	 * 
	 */
	@Transactional
	public List<Generatedproduct> findAllGeneratedproducts(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Generatedproduct>(generatedproductDAO.findAllGeneratedproducts(startResult, maxRows));
	}

	/**
	 * Return a count of all Generatedproduct entity
	 * 
	 */
	@Transactional
	public Integer countGeneratedproducts() {
		return ((Long) generatedproductDAO.createQuerySingleResult("select count(o) from Generatedproduct o").getSingleResult()).intValue();
	}

	/**
	 * Save an existing Thesis entity
	 * 
	 */
	@Transactional
	public Generatedproduct saveGeneratedproductThesises(Integer generatedProductId, Thesis related_thesises) {
		Generatedproduct generatedproduct = generatedproductDAO.findGeneratedproductByPrimaryKey(generatedProductId, -1, -1);
		Thesis existingthesises = thesisDAO.findThesisByPrimaryKey(related_thesises.getThesisId());

		// copy into the existing record to preserve existing relationships
		if (existingthesises != null) {
			existingthesises.setThesisId(related_thesises.getThesisId());
			existingthesises.setTitle(related_thesises.getTitle());
			existingthesises.setAbstract_(related_thesises.getAbstract_());
			existingthesises.setProposalAttempts(related_thesises.getProposalAttempts());
			existingthesises.setDefenseAttempts(related_thesises.getDefenseAttempts());
			existingthesises.setStartDate(related_thesises.getStartDate());
			existingthesises.setDoi(related_thesises.getDoi());
			related_thesises = existingthesises;
		} else {
			related_thesises = thesisDAO.store(related_thesises);
			thesisDAO.flush();
		}

		generatedproduct.getThesises().add(related_thesises);
		related_thesises.getGeneratedproducts().add(generatedproduct);
		generatedproduct = generatedproductDAO.store(generatedproduct);
		generatedproductDAO.flush();

		related_thesises = thesisDAO.store(related_thesises);
		thesisDAO.flush();

		return generatedproduct;
	}

	/**
	 * Save an existing Generatedproduct entity
	 * 
	 */
	@Transactional
	public void saveGeneratedproduct(Generatedproduct generatedproduct) {
		Generatedproduct existingGeneratedproduct = generatedproductDAO.findGeneratedproductByPrimaryKey(generatedproduct.getGeneratedProductId());

		if (existingGeneratedproduct != null) {
			if (existingGeneratedproduct != generatedproduct) {
				existingGeneratedproduct.setGeneratedProductId(generatedproduct.getGeneratedProductId());
				existingGeneratedproduct.setProductName(generatedproduct.getProductName());
				existingGeneratedproduct.setProductDescription(generatedproduct.getProductDescription());
			}
			generatedproduct = generatedproductDAO.store(existingGeneratedproduct);
		} else {
			generatedproduct = generatedproductDAO.store(generatedproduct);
		}
		generatedproductDAO.flush();
	}
}
